<template>
  <view class="command-palette" v-if="visible">
    <view class="command-palette-header">
      <text class="palette-title">💬 快速指令</text>
      <text class="palette-hint">↑↓ 选择  Enter 执行  Esc 取消</text>
    </view>
    <view class="command-palette-list">
      <view 
        v-for="(cmd, index) in filteredCommands" 
        :key="cmd.command"
        class="palette-command-item"
        :class="{ active: selectedIndex === index }"
        @click.stop="onExecute(cmd)"
        @mousedown.stop.prevent
      >
        <text class="palette-cmd-icon">{{ cmd.icon }}</text>
        <text class="palette-cmd-label">{{ cmd.label }}</text>
      </view>
      <view class="palette-empty" v-if="filteredCommands.length === 0">
        <text>未找到匹配的指令</text>
      </view>
    </view>
  </view>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'CommandPalette',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    searchText: {
      type: String,
      default: ''
    },
    selectedIndex: {
      type: Number,
      default: 0
    }
  },
  emits: ['execute'],
  setup(props, { emit }) {
    // 命令列表
    const commandList = [
      { icon: '🔄', label: '预报:切换预报方案', command: 'switch_scheme' },
      { icon: '⚙️', label: '预报:时间修改', command: 'config_time' },
      { icon: '📝', label: '预报:参数修改', command: 'view_params' },
      { icon: '🌧️', label: '预报:降雨修改', command: 'edit_rainfall' },
      { icon: '📊', label: '预报:结果查询', command: 'view_result' }
    ]
    
    // 过滤后的命令列表（支持模糊搜索）
    const filteredCommands = computed(() => {
      if (!props.visible) return []
      
      const search = props.searchText.toLowerCase()
      if (!search) {
        return commandList
      }
      
      return commandList.filter(cmd => 
        cmd.label.toLowerCase().includes(search)
      )
    })
    
    const onExecute = (cmd) => {
      emit('execute', cmd)
    }
    
    return {
      filteredCommands,
      onExecute
    }
  }
}
</script>

<style scoped>
/* 命令面板 */
.command-palette {
  position: absolute;
  bottom: 100%;
  left: 20px;
  right: 20px;
  margin-bottom: 8px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  z-index: 1000;
  animation: slideUp 0.2s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.command-palette-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
}

.palette-title {
  font-size: 14px;
  font-weight: 600;
}

.palette-hint {
  font-size: 11px;
  opacity: 0.9;
}

.command-palette-list {
  max-height: 300px;
  overflow-y: auto;
}

.palette-command-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  border-bottom: 1px solid #f0f0f0;
}

.palette-command-item:last-child {
  border-bottom: none;
}

.palette-command-item:hover {
  background-color: #f5f7fa;
}

.palette-command-item.active {
  background: linear-gradient(90deg, #e3f2fd 0%, #f3e5f5 100%);
  border-left: 3px solid #667eea;
}

.palette-cmd-icon {
  font-size: 20px;
  flex-shrink: 0;
}

.palette-cmd-label {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  flex: 1;
}

.palette-empty {
  padding: 40px 20px;
  text-align: center;
  color: #909399;
  font-size: 13px;
}
</style>

